package com.pureut.storage.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.pureut.storage.dto.*;
import com.pureut.storage.export.WipCompletionExport;
import com.pureut.storage.service.WipCompletionService;
import com.pureut.storage.util.ExcelUtil;
import com.pureut.storage.vo.*;
import com.sale.common.core.domain.R;
import com.sale.common.core.web.controller.BaseController;
import com.sale.common.log.annotation.Log;
import com.sale.common.log.enums.BusinessType;
import com.sale.common.mybatis.core.page.PageQuery;
import com.sale.common.mybatis.core.page.TableDataInfo;
import com.sale.storage.api.dto.UpdateWipByAdjustmentVo;
import com.sale.storage.api.dto.WipCompletionMessageDto;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/***
 * 完工入库控制层
 * Author:C
 * Date:2022/12/30 11:56
 * @DESC
 */
@RestController
@RequestMapping("/wip-completion")
public class WipCompletionController extends BaseController {


    @Resource
    WipCompletionService wipCompletionService;


    /**
     * 获取完工入库分页列表
     */
    //@SaCheckPermission("warehouse:warehousing:finished:list")
    @GetMapping("/list")
    public TableDataInfo<WipCompletionDto> getList(WipCompletionVo wipCompletionVo, PageQuery pageQuery) throws Exception {
        //查询完工入库列表
        return wipCompletionService.getReceiptList(wipCompletionVo, pageQuery);
    }

    /**
     * 新增完工入库
     */
    //@SaCheckPermission("warehouse:warehousing:finished:add")
    @Log(title = "新增完工入库", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public R<Void> wipAdd(@Validated @RequestBody WipCompletionVo wipCompletionVo) throws Exception {
        return toAjax(wipCompletionService.addWipCompletion(wipCompletionVo));
    }

//    /**
//     * 新增完工入库（其他模块调用）
//     */
//    @PostMapping("/wipOtherAdd")
//    public R<Void> wipOtherAdd(@Validated @RequestBody WipOtherAddVo wipOtherAddVo) throws Exception {
//        return toAjax(wipCompletionService.wipOtherAdd(wipOtherAddVo));
//    }

    /**
     * 关结完工入库
     */
    //@SaCheckPermission("warehouse:warehousing:finished:closing")
    @Log(title = "关结完工入库", businessType = BusinessType.UPDATE)
    @PostMapping("/close")
    public R<Void> close(@RequestBody WipCompletionVo wipCompletionVo) {
        return toAjax(wipCompletionService.close(wipCompletionVo));
    }

    /**
     * 查看
     */
    //@SaCheckPermission("warehouse:warehousing:finished:query")
    @GetMapping("/view/{id}")
    public R<WipCompletionDto> view(@PathVariable("id") Long id) {
        return R.ok(wipCompletionService.getView(id));
    }

    /**
     * 修改完工入库
     */
    //@SaCheckPermission("warehouse:warehousing:finished:edit")
    @Log(title = "修改完工入库", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public R<Void> updateWipCompletion(@Validated @RequestBody WipCompletionVo wipCompletionVo) {
        return toAjax(wipCompletionService.updateWipCompletion(wipCompletionVo));
    }

    /**
     * 删除完工入库
     */
    //@SaCheckPermission("warehouse:warehousing:finished:remove")
    @Log(title = "删除完工入库", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{idStr}")
    public R<Void> deleteWipCompletion(@PathVariable("idStr") String idStr) {
        return toAjax(wipCompletionService.deleteWipCompletion(idStr));
    }

    /**
     * 导出
     */
    //@SaCheckPermission("warehouse:warehousing:finished:export")
    @PostMapping("/wipCompletionMessageExport")
    public R<Void> wipCompletionMessageExport(HttpServletResponse response, WipCompletionVo wipCompletionVo) throws IOException {
        List<WipCompletionExport> infoExport = wipCompletionService.getInfoExport(wipCompletionVo);
        ExcelUtil<WipCompletionExport> excelUtil = new ExcelUtil<WipCompletionExport>(WipCompletionExport.class);
        excelUtil.excelWriter(response, infoExport, "完工入库信息.xlsx");
        return R.ok();
    }


//    /**
//     * 完工入库（pda）生成方式为自动生成
//     */
//    //@SaCheckPermission("warehouse:warehousing:finished:warehousing")
//    @PostMapping("/wipCompletionPDA")
//    @Log(title = "完工入库（pda）生成方式为自动生成", businessType = BusinessType.UPDATE)
//    public R<PickingRestDbDto> autWipCompletionPda(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
//        return R.ok(wipCompletionService.wipCompletionPda(pickingRestDbVo));
//    }

    /**
     * 完工入库（pda）生成方式为手动生成
     */
    //@SaCheckPermission("warehouse:warehousing:finished:warehousing")
    @PostMapping("/manualWipCompletionPDA")
    @Log(title = "完工入库（pda）生成方式为手动生成", businessType = BusinessType.UPDATE)
    public R<PickingRestDbDto> manualWipCompletionPda(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return R.ok(wipCompletionService.manualWipCompletionPda(pickingRestDbVo));
    }

    /**
     * 完工入库拣货撤销
     */
    @PostMapping("/pickingCancellation")
    @Log(title = "完工入库拣货撤销", businessType = BusinessType.UPDATE)
    public R<Void> pickingCancellation(@RequestBody PickingRestDbVo pickingRestDbVo) {
        return toAjax(wipCompletionService.pickingCancellation(pickingRestDbVo));
    }

    /**
     * 完工入库拣货完成
     */
    @PostMapping("/pickingCompleted")
//    @Log(title = "完工入库拣货完成", businessType = BusinessType.UPDATE)
    public R<Void> pickingCompleted(@RequestBody PickingRestDbVo pickingRestDbVo) throws Exception {
        return toAjax(wipCompletionService.pickingCompleted(pickingRestDbVo));
    }

    /**
     * 完工入库打印
     */
    @PostMapping("/wipPrinting")
    @Log(title = "完工入库打印", businessType = BusinessType.INSERT)
    public R<Void> wipPrinting(@RequestBody WarehousInspectionVo warehousInspectionVo) throws Exception {
        return toAjax(wipCompletionService.wipPrinting(warehousInspectionVo));
    }

    /**
     * 根据单据号获取完工入库信息（成本调整使用）
     */
    @GetMapping("/getWipCompletionMessage")
    public R<WipCompletionMessageDto> getWipCompletionMessage(String documentNumber) {
        return R.ok(wipCompletionService.getWipCompletionMessage(documentNumber));
    }

    /**
     * 修改完工入库信息（成本调整使用）
     */
    @PutMapping("/updateWipByAdjustment")
    public R<Void> updateWipByAdjustment(@RequestBody UpdateWipByAdjustmentVo updateWipByAdjustmentVo) {
        return toAjax(wipCompletionService.updateWipByAdjustment(updateWipByAdjustmentVo));
    }

    /**
     * 获取完工入库单中的入库仓库(成本调整使用)
     */
    @GetMapping("/getWarehouseIn")
    public R<List<HashMap<String, String>>> getWarehouseIn() {
        List<HashMap<String, String>> resultMap = new ArrayList<>();
        List<WarehouseInDto> warehouseIn = wipCompletionService.getWarehouseIn();
        for (WarehouseInDto entity : warehouseIn) {
            HashMap<String, String> map = new HashMap<>();
            map.put("label", entity.getWareHouseName());
            map.put("value", String.valueOf(entity.getWarehouseId()));
            resultMap.add(map);
        }
        return R.ok(resultMap);
    }

    /**
     * 筛选的仓库单据为已完成或已关结的完工入库单(成本调整使用)
     */
    @GetMapping("/screenDocument")
    public TableDataInfo<WipCompletionDto> screenDocument(WipCompletionVo wipCompletionVo, PageQuery pageQuery) {
        return wipCompletionService.screenDocument(wipCompletionVo, pageQuery);
    }


//    /**
//     * 完工入库品质回传状态(fegin调用)
//     */
//    @PostMapping("/updatewipCompletionByQuality")
//    public R<Void> updatewipCompletionByQuality(@RequestBody UpdateStatusByQualityVo updateStatusByQualityVo) {
//        return toAjax(wipCompletionService.updateStatusByQuality(updateStatusByQualityVo));
//    }

//    /**
//     * 完工入库品质回传状态(品质处理fegin调用)
//     */
//    @PostMapping("/wipCompletionQualityStatus")
//    public R<Void> wipCompletionQualityStatus(@RequestBody ReturnStatusVo returnStatusVo) {
//        return toAjax(wipCompletionService.wipCompletionQualityStatus(returnStatusVo));
//    }


    /**
     * 根据物料编码获取采购入库及完工入库已完成、已关节状态单据信息
     */
    @GetMapping("/getPrintDocument")
    public R<List<PrintDocumentDto>> getPrintDocument(String materialCode) {
        return R.ok(wipCompletionService.getPrintDocument(materialCode));
    }

}
